package com.puff.controller.platform;

import com.puff.dto.DishDTO;
import com.puff.dto.ShopDTO;
import com.puff.dto.ShopPageQueryDTO;
import com.puff.entity.Dish;
import com.puff.entity.Shop;
import com.puff.entity.ShopCategory;
import com.puff.result.PageResult;
import com.puff.result.Result;
import com.puff.service.ShopService;
import com.puff.vo.DishVO;
import com.puff.vo.ShopVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("platformShopController")
@RequestMapping("/platform/shop")
@Api(tags = "商家管理相关接口")
@Slf4j
public class ShopController {

    @Autowired
    private ShopService shopService;

    private static final String KEY = "SHOP_STATUS_";
    private static final String PERMISSION = "PLAT_STATUS_";

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 员工列表分页查询
     * @param shopPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "平台端商家分页查询")
    public Result<PageResult> page(ShopPageQueryDTO shopPageQueryDTO){
        log.info("平台端商家分页查询:{}",shopPageQueryDTO);
        // 由于是query查询而不是json，因此不需要加@RequestBody
        PageResult pageResult = shopService.pageQuery(shopPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增商家
     * @param shopDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增商家")
    public Result save(@RequestBody ShopDTO shopDTO){
        log.info("新增商家: {}",shopDTO);
        shopService.saveShop(shopDTO);

        //TODO 缓存处理

        // 清理缓存数据
        //String key = "dish_" + shopDTO.getCategoryId();
        //cleanCache(key);
        return Result.success();
    }

    /**
     * 批量删除商家信息
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除商家信息")
    public Result delete(@RequestParam List<Long> ids){
        // @RequestParam注解能够让mybatis去解析前端发送过来的字符串，并添加到ids中
        log.info("批量删除商家信息:{}",ids);
        shopService.deleteBatch(ids);

        //TODO 缓存处理

        // 将所有菜品缓存数据都清理掉，所有以dish_开头的key
        //cleanCache("dish_*");

        return Result.success();
    }

    /**
     * 根据id查询商家信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询商家信息")
    public Result<ShopVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品信息:{}",id);
        ShopVO shopVO = shopService.getShopById(id);
        return Result.success(shopVO);
    }

    /**
     * 修改商家信息
     * @param shopDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改商家信息")
    public Result update(@RequestBody ShopDTO shopDTO){
        log.info("修改商家信息:{}",shopDTO);
        shopService.updateShop(shopDTO);

        //TODO 缓存处理

        // 将所有菜品缓存数据都清理掉，所有以dish_开头的key
        //cleanCache("dish_*");

        return Result.success();
    }

    /**
     * 商家打烊中/营业中切换
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("商家打烊中/营业中切换")
    public Result startOrStop(Long id,@PathVariable Integer status){
        log.info("商家打烊中/营业中切换:{},{}",id,status);
        shopService.startOrStopShop(id,status);

        //TODO 商家营业状态修改，并协调修改Mysql和Redis中的数据
        log.info("设置店铺的营业状态为：{}",status == 1 ? "营业中" : "打烊中");
        redisTemplate.opsForValue().set(KEY+id.toString(),status);
//        if (status == 0){
//            redisTemplate.opsForValue().set(PERMISSION + id,0);
//        }else {
//            redisTemplate.opsForValue().set(PERMISSION + id,1);
//        }

        // 将所有菜品缓存数据都清理掉，所有以dish_开头的key
        //cleanCache("dish_*");

        return Result.success();
    }
}
